using System.Data;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace ODX.Core.Mobile
{
    [WebServiceBinding(Namespace = "http://codeplex.com/odx")]
    public class DbDataSourceWebProxy : SoapHttpClientProtocol, ISqlDataProvider
    {
        public DbDataSourceWebProxy(string url)
        {
            Url = url;
        }

        [SoapDocumentMethod("http://codeplex.com/odx/Select")]
        public DataSet Select(string tableName, string filter, string sort, int count, object[] parameters)
        {
            object[] results = Invoke("Select", new object[] {
                        tableName,
                        filter,
                        sort,
                        count,
                        parameters});

            return (DataSet)(results[0]);
        }

        [SoapDocumentMethod("http://codeplex.com/odx/GetRecord")]
        public DataSet GetRecord(string tableName, string ID)
        {
            object[] results = Invoke("GetRecord", new object[] {
                        tableName,
                        ID});
            return (DataSet)(results[0]);
        }


        [SoapDocumentMethod("http://codeplex.com/odx/GetChildren")]
        public DataSet GetChildren(string childTableName, string childFK, string parentID)
        {
            object[] results = Invoke("GetChildren", new object[] {
                        childTableName,
                        childFK,
                        parentID});
            return (DataSet)(results[0]);
        }


        [SoapDocumentMethod("http://codeplex.com/odx/GetAssoc")]
        public DataSet GetAssoc(string linkTableName, string childTableName, string parentFK, string childFK, string parentID)
        {
            object[] results = Invoke("GetAssoc", new object[] {
                        linkTableName,
                        childTableName,
                        parentFK,
                        childFK,
                        parentID});
            return (DataSet)(results[0]);
        }

        public NullsPosition NullsPosition
        {
            get { return NullsPosition.Minimum; }
        }

        [SoapDocumentMethod("http://codeplex.com/odx/Save")]
        public void Save(DataSet changes, SaveMode saveMode)
        {
            Invoke("Save", new object[] {
                        changes,
                        saveMode});
        }

        [SoapDocumentMethod("http://codeplex.com/odx/SelectSql")]
        public DataSet SelectSql(string selectSql, string tableName, int count, params object[] parameters)
        {
            object[] results = Invoke("SelectSql", new object[] {
                        selectSql,
                        tableName,
                        count,
                        parameters});

            return (DataSet) (results[0]);
        }
    }
}
